package me.snowiest.snowdrift.vo;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import me.snowiest.snowdrift.constant.ResultCode;

import java.util.HashMap;
import java.util.Map;

@Data
public class CommonResult {
    @JSONField(serialize = false)
    private boolean success;
    @JSONField(ordinal = 1)
    private Integer code;
    @JSONField(name = "msg",ordinal = 2)
    private String message;
    @JSONField(ordinal = 3)
    private Map<String, Object> data;

    private CommonResult() {
    }


    public static CommonResult success(int code, String message, Map<String, Object> data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(true);
        commonResult.setCode(code);
        commonResult.setMessage(message);
        commonResult.setData(data);
        return commonResult;
    }

    public static CommonResult success() {
        return success(ResultCode.SUCCESS, "success");
    }

    public static CommonResult success(int code, String message) {
        return success(code, message, new HashMap<>());
    }

    public static CommonResult fail(int code, String message, Map<String, Object> data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(false);
        commonResult.setCode(code);
        commonResult.setMessage(message);
        commonResult.setData(data);
        return commonResult;
    }

    public static CommonResult fail() {
        return fail(ResultCode.FAIL, "fail");
    }

    public static CommonResult fail(int code, String message) {
        return fail(code, message, new HashMap<>());
    }

    public CommonResult code(int code) {
        this.code = code;
        return this;
    }

    public CommonResult message(String message) {
        this.message = message;
        return this;
    }

    public CommonResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public CommonResult data(Map<String, Object> dataMap) {
        this.data = dataMap;
        return this;
    }
}
